Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Configuration.ConfigurationSettings
Imports System.Globalization

Public Class CIMSfunc
    Private colControl As Collection

    Public Sub New()
        colControl = New Collection
    End Sub

    Public Function ParsePhoneNo(ByVal PhoneNo As String, ByVal Index As Integer)
        Dim iDX As Integer = 1
        Dim strPhone As String = Trim(PhoneNo) & "-"
        Dim strReturn As String = ""

        Do Until Index = 1
            iDX = InStr(strPhone, "-")
            strPhone = Mid(strPhone, iDX + 1, Len(strPhone))

            Index -= 1
        Loop

        iDX = InStr(strPhone, "-")
        strPhone = Mid(strPhone, 1, iDX - 1)

        Return strPhone
    End Function

    Public Function ParseGetDate(ByVal strDate As String)
        Dim strReturn As String = ""

        If CDate(strDate) <> CDate("09/09/9999") And IsDate(strDate) Then
            strReturn = Format(CDate(strDate), "dd/MM/yyyy")
        End If

        Return strReturn
    End Function

    Public Function ParseSetDate(ByVal strDate As String)
        Dim strReturn As String = strDate

        If strDate = "" Then
            strReturn = "09/09/9999"
        Else
            strReturn = FDate(strDate)
        End If

        Return strReturn
    End Function

    Function FDate(ByVal strDate As String) As Date
        Dim culture = New CultureInfo("fr-FR", True)
        Dim rDate As DateTime

        If strDate <> "" Then
            rDate = _
                 DateTime.Parse(strDate, _
                                culture, _
                                DateTimeStyles.NoCurrentDateDefault)
        End If

        Return rDate
    End Function

    Public Function GetLastID(ByVal IDField As String, ByVal Table As String) As String
        Dim dao As New DAOMain.CommonDAO
        Dim strReturn As String

        Dim dt As DataTable = dao.ExecQuery("Select isnull(max(convert(numeric, " & IDField & ")),0) +1 as ID From " & Table)

        strReturn = dt.Rows(0).Item("ID")
        Return strReturn
    End Function

    Public Function GetChValue(ByVal Value As Boolean) As Integer
        Select Case Value
            Case True
                Return 1
            Case False
                Return 0
        End Select
    End Function

    Public Function GetProvider(ByVal Seq_Cd As String) As String
        Dim vo As New DAOMain.VW_POLICY_TRANS_SEARCH_VO
        Dim dao As New DAOMain.VW_POLICY_TRANS_SEARCH_DAO
        Dim strReturn As String = ""

        vo.POLICY_SEQ_CD = Seq_Cd
        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                strReturn = .Item("PROVIDER_DESC") & " - " & .Item("PRDT_DESC")
            End With
        End If

        vo = Nothing
        dao = Nothing

        Return strReturn
    End Function

    Public Sub AddExemptControl(ByVal myControl As Control)
        colControl.Add(myControl)
    End Sub

    Public Sub FormDisable(ByVal Parent As Object)
        Dim ctrl As Control

        For Each ctrl In Parent.Controls

            If TypeOf ctrl Is TextBox Then
                Dim txt As TextBox = ctrl
                txt.Enabled = False
                txt = Nothing
            ElseIf TypeOf ctrl Is DropDownList Then
                Dim ddl As DropDownList = ctrl
                ddl.Enabled = False
                ddl = Nothing
            ElseIf TypeOf ctrl Is CheckBox Then
                Dim ch As CheckBox = ctrl
                ch.Enabled = False
                ch = Nothing
            ElseIf TypeOf ctrl Is Button Then
                Dim btn As Button = ctrl

                If btn.ID = "Save_btn" Then
                    btn.Visible = False
                    btn = Nothing
                End If

                'ElseIf TypeOf ctrl Is LinkButton Then
                '    Dim lbtn As LinkButton = ctrl
                '    Dim blnfound As Boolean = False
                '    'Dim i As Integer

                '    'For i = 1 To colControl.Count
                '    '    If colControl.Item(i).ID = lbtn.ID Then
                '    '        blnfound = True
                '    '        colControl.Remove(i)
                '    '    End If
                '    'Next i

                '    'If blnfound = False Then lbtn.Visible = False
                '    lbtn.Visible = False
            End If
        Next

        ctrl = Nothing
    End Sub

    Public Function GetSMTPServer() As String
        Dim strSMTP As String = AppSettings("SMTP")

        Return strSMTP
    End Function

    Public Function GetAdminEmail() As String
        Dim strAdminEmail As String = AppSettings("AdminEmail")

        Return strADminEmail
    End Function

    Protected Overrides Sub Finalize()
        colControl = Nothing
        MyBase.Finalize()
    End Sub

    Public Sub setNumericFormat(ByVal txt As TextBox)
        txt.Attributes.Add("onblur", "lostFocus(this)")
        txt.Attributes.Add("onfocus", "gotFocus(this)")
        txt.Attributes.Add("onKeyPress", "return checkDecimal(this);")
    End Sub

    Public Function saveFormat(ByVal mNumber As String)
        If mNumber = "" Then mNumber = 0
        Dim txt As String = Format(CDec(mNumber), "####.#0")
        Return txt
    End Function

    Public Function loadFormat(ByVal mNumber As String)
        If mNumber = "" Then mNumber = 0
        Dim txt As String = Format(CDec(mNumber), "#,##0.00")
        Return txt
    End Function

    Public Function cNumToWord(ByVal src_num As String) As String
        Dim SNUM As Double
        SNUM = Val(src_num)
        If SNUM > 999999999999999.0# Then
            cNumToWord = "Error: To much number."
            Exit Function
        End If
        Dim WHOLE As String
        Dim EXTRA As String
        Dim WORD As String
        Dim NWHOLE As Double

        If InStr(1, Str$(SNUM), ".", vbTextCompare) <> 0 Then
            WHOLE = Split(Str$(SNUM), ".")(0)
            EXTRA = Split(src_num, ".")(1)
        Else
            WHOLE = SNUM
        End If

        If SNUM < 1 Then WORD = "Zero"

        NWHOLE = Val(WHOLE)
        'Check for One and Tens
        If Val(Right(NWHOLE, 2)) > 0 And Val(Right(NWHOLE, 2)) < 21 Or Val(Right(NWHOLE, 2)) = 30 Or Val(Right(NWHOLE, 2)) = 40 Or Val(Right(NWHOLE, 2)) = 50 Or Val(Right(NWHOLE, 2)) = 60 Or Val(Right(NWHOLE, 2)) = 70 Or Val(Right(NWHOLE, 2)) = 80 Or Val(Right(NWHOLE, 2)) = 90 Then
            WORD = WORD & WordTens(Val(Right(NWHOLE, 2)))
        ElseIf Val(Right(NWHOLE, 2)) > 20 Then
            WORD = WORD & WordTens(Left(Right(NWHOLE, 2), 1) & "0")
            WORD = WORD & WordTens(Right(NWHOLE, 1))
        End If
        'Check for Hundred
        If NWHOLE > 99 Then
            If Left(Right(NWHOLE, 3), 1) <> "0" Then WORD = WordTens(Left(Right(NWHOLE, 3), 1)) & " Hundred" & WORD
        End If
        'Check for Thousand
        If NWHOLE > 999 Then
            If Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) > 0 And Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) < 21 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 30 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 40 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 50 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 60 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 70 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 80 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) = 90 Then
                WORD = WordTens(Val(Left(NWHOLE, Len("" & NWHOLE) - 3))) & " Thousand" & WORD
            ElseIf Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) > 20 And Right(Left(NWHOLE, Len("" & NWHOLE) - 3), 3) <> "000" Then
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 3)), 2), 2, 1)) & " Thousand" & WORD
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 3)), 2), 1, 1) & "0") & WORD
                If Val(Left(NWHOLE, Len("" & NWHOLE) - 3)) > 99 Then
                    If Left(Right(NWHOLE, 6), 1) <> "0" Then WORD = WordTens(Left(Right(NWHOLE, 6), 1)) & " Hundred" & WORD
                End If
            End If
        End If
        'Check for Million
        If NWHOLE > 999999 Then
            If Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) > 0 And Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) < 21 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 30 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 40 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 50 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 60 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 70 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 80 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) = 90 Then
                WORD = WordTens(Val(Left(NWHOLE, Len("" & NWHOLE) - 6))) & " Million" & WORD
            ElseIf Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) > 20 And Right(Left(NWHOLE, Len("" & NWHOLE) - 6), 3) <> "000" Then
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 6)), 2), 2, 1)) & " Million" & WORD
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 6)), 2), 1, 1) & "0") & WORD
                If Val(Left(NWHOLE, Len("" & NWHOLE) - 6)) > 99 Then
                    If Left(Right(NWHOLE, 9), 1) <> "0" Then WORD = WordTens(Left(Right(NWHOLE, 9), 1)) & " Hundred" & WORD
                End If
            End If
        End If
        'Check for Billion
        If NWHOLE > 999999999 Then
            If Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) > 0 And Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) < 21 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 30 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 40 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 50 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 60 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 70 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 80 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) = 90 Then
                WORD = WordTens(Val(Left(NWHOLE, Len("" & NWHOLE) - 9))) & " Billion" & WORD
            ElseIf Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) > 20 And Right(Left(NWHOLE, Len("" & NWHOLE) - 9), 3) <> "000" Then
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 9)), 2), 2, 1)) & " Billion" & WORD
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 9)), 2), 1, 1) & "0") & WORD
                If Val(Left(NWHOLE, Len("" & NWHOLE) - 9)) > 99 Then
                    If Left(Right(NWHOLE, 12), 1) <> "0" Then WORD = WordTens(Left(Right(NWHOLE, 12), 1)) & " Hundred" & WORD
                End If
            End If
        End If
        'Check for Trillion
        If NWHOLE > 999999999999.0# Then
            If Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) > 0 And Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) < 21 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 30 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 40 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 50 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 60 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 70 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 80 Or Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) = 90 Then
                WORD = WordTens(Val(Left(NWHOLE, Len("" & NWHOLE) - 12))) & " Trillion" & WORD
            ElseIf Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) > 20 And Right(Left(NWHOLE, Len("" & NWHOLE) - 12), 3) <> "000" Then
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 12)), 2), 2, 1)) & " Trillion" & WORD
                WORD = WordTens(Mid(Right(Val(Left(NWHOLE, Len("" & NWHOLE) - 12)), 2), 1, 1) & "0") & WORD
                If Val(Left(NWHOLE, Len("" & NWHOLE) - 12)) > 99 Then
                    If Left(Right(NWHOLE, 15), 1) <> "0" Then WORD = WordTens(Left(Right(NWHOLE, 15), 1)) & " Hundred" & WORD
                End If
            End If
        End If
        If EXTRA = "" Then
            WORD = WORD & "   and   00/100"
        Else
            If Val(EXTRA) < 10 Then EXTRA = "0" & EXTRA
            WORD = WORD & "   and   " & EXTRA & "/100"
        End If
        cNumToWord = WORD

        NWHOLE = 0
        WORD = ""
        EXTRA = ""
        WHOLE = ""
    End Function

    Private Function WordTens(ByVal SNUM As Long) As String
        Select Case SNUM
            Case 1
                WordTens = " One"
            Case 2
                WordTens = " Two"
            Case 3
                WordTens = " Three"
            Case 4
                WordTens = " Four"
            Case 5
                WordTens = " Five"
            Case 6
                WordTens = " Six"
            Case 7
                WordTens = " Seven"
            Case 8
                WordTens = " Eight"
            Case 9
                WordTens = " Nine"
            Case 10
                WordTens = " Ten"
            Case 11
                WordTens = " Eleven"
            Case 12
                WordTens = " Twelve"
            Case 13
                WordTens = " Thirteen"
            Case 14
                WordTens = " Fourteen"
            Case 15
                WordTens = " Fifteen"
            Case 16
                WordTens = " Sixteen"
            Case 17
                WordTens = " Seventeen"
            Case 18
                WordTens = " Eighteen"
            Case 19
                WordTens = " Nineteen"
            Case 20
                WordTens = " Twenty"
            Case 30
                WordTens = " Thirty"
            Case 40
                WordTens = " Fourty"
            Case 50
                WordTens = " Fifty"
            Case 60
                WordTens = " Sixty"
            Case 70
                WordTens = " Seventy"
            Case 80
                WordTens = " Eighty"
            Case 90
                WordTens = " Ninty"
        End Select
    End Function
End Class
